#!/bin/bash

# 设置测试参数
BASE_URL="http://localhost:8080"
CONCURRENT_USERS=100
TEST_DURATION=300  # 5分钟
RAMP_UP=60        # 1分钟爬升时间

# 安装依赖
go install github.com/tsenart/vegeta@latest

# 生成测试数据
cat > targets.txt << EOF
POST $BASE_URL/api/auth/login
Content-Type: application/json
@data.json

GET $BASE_URL/api/user/info
Authorization: Bearer {{.token}}

GET $BASE_URL/api/stats/overview
Authorization: Bearer {{.token}}

POST $BASE_URL/api/message/send
Content-Type: application/json
Authorization: Bearer {{.token}}
@message.json
EOF

# 创建测试数据文件
cat > data.json << EOF
{
  "username": "testuser",
  "password": "testpass"
}
EOF

cat > message.json << EOF
{
  "robot_id": "test-robot-001",
  "type": "text",
  "content": "test message"
}
EOF

# 运行压力测试
echo "开始压力测试..."
echo "并发用户数: $CONCURRENT_USERS"
echo "测试时长: $TEST_DURATION 秒"
echo "爬升时间: $RAMP_UP 秒"

vegeta attack -targets=targets.txt \
  -rate=$CONCURRENT_USERS \
  -duration=${TEST_DURATION}s \
  -ramp-up=${RAMP_UP}s \
  | tee results.bin \
  | vegeta report

# 生成HTML报告
vegeta plot results.bin > report.html

echo "压力测试完成"
echo "详细报告已保存到 report.html" 